﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using MethodIT.Helper;
using MyGeneration.dOOdads;

using Hr20.DAL;
using Hr20.BLL;
using Hr20.ProcessFlow;

namespace Appraisal.Web.Admin
{
    public partial class NewUmp : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                int ID = HelperURL.GetIntFromUrlString("ID");
                if (ID != -1)
                {
                    txtBirthDay.Text = "1980-1-1";
                    Bind(ID);
                }
            }
        }

        private void Bind(int ID)
        {
            ZB_Ump b = new ZB_Ump();
            if (b.LoadByPrimaryKey(ID))
            {
                txtName.Text = b.Name;
                chkEnable.Checked = b.Enable.Equals("Y");

                txtCode.Text = b.Code;
                txtCardCode.Text = b.CardNo;
                FunctionSet.SetListIndex(rblType,b.UmpType);
                FunctionSet.SetListIndex(rblSex,b.Sex);
                txtCorpName.Text = b.CorpName;
                txtPhone.Text = b.Phone;
                txtAddress1.Text = b.Address;
                txtAddress2.Text = b.Address1;
                txtBirthDay.Text = b.Birthday.ToShortDateString();
                txtCity.Text = b.City;
                txtRelation.Text = b.Relation;
                txtDesc.Text = b.UmpDesc;
                txtRemark.Text = b.Remark;
            }
        }

        private void Save()
        {
            int ID = HelperURL.GetIntFromUrlString("ID");
            ZB_Ump b = new ZB_Ump();
            b.Where.Name.Value = txtName.Text;
            b.Where.ID.Value = ID;
            b.Where.ID.Operator = WhereParameter.Operand.NotEqual;
            //检查名称是否重复
            if (b.Query.Load())
            {
                HelperMessage.Message("姓名重复!");
                return;
            }

            //保存
            if (!b.LoadByPrimaryKey(ID))
            {
                b.AddNew();
                b.ID = SequenceGenerator.GetNextVal("Ump");
            }
            b.Name = txtName.Text;
            b.Enable = chkEnable.Checked ? "Y" : "N";

            b.Code = txtCode.Text ;
            b.CardNo = txtCardCode.Text;
            b.UmpType= rblType.SelectedValue ;
            b.Sex = rblSex.SelectedValue;
            b.CorpName = txtCorpName.Text ;
            b.Phone = txtPhone.Text ;
            b.Address = txtAddress1.Text  ;
            b.Address1= txtAddress2.Text ;
            b.Birthday = DateTime.Parse(txtBirthDay.Text);
            b.City = txtCity.Text ;
            b.Relation = txtRelation.Text ;
            b.UmpDesc = txtDesc.Text  ;
            b.Remark = txtRemark.Text ;

            b.Save();
            HelperURL.Redirect("UmpAdmin.aspx");
        }

        protected void lbSave_Click(object sender, EventArgs e)
        {
            Save();
        }

        protected void lbSaveBottom_Click(object sender, EventArgs e)
        {
            Save();
        }
    }
}
